﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.Security.Cryptography;
using System.Collections;


namespace eVoting
{
    public partial class Registration : System.Web.UI.Page
    {
        DBManager db;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBManager();

            if (!IsPostBack)
            {
                List<StudyGroup> SGs = db.getAllStudyGroups();

                foreach (StudyGroup sg in SGs)
                {
                    StudyGroup.Items.Add(new ListItem((sg.Faculty1.name + " " + st(sg.year) + " Year"), sg.ID + ""));
                }
            }
        }

        protected String PasswordHashing()
        {
            string message;
            message = "";// this.CreateUserWizard1.Password;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            //MD5 md5 = new MD5CryptoServiceProvider();
            //SHA1 sha1 = new SHA1CryptoServiceProvider();
            SHA256Managed sha256 = new SHA256Managed();
            //SHA384Managed sha384 = new SHA384Managed();
            //SHA512Managed sha512 = new SHA512Managed();

            //md5($salt.md5($pass))
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = sha256.ComputeHash(messageBytes);
            byte[] saltedhash;

            //hashmessage = md5.ComputeHash(messageBytes);
            hashmessage = sha256.ComputeHash(messageBytes);
            // aDD salt.
            string enc = "";
            enc = message + System.Convert.ToBase64String(hashmessage);

            // Get the new byte array after adding the salt.
            saltedhash = sha256.ComputeHash(encoding.GetBytes(enc));

            String hashedPassword = Convert.ToBase64String(saltedhash);
            return hashedPassword;
        }


        protected void CreateUserWizard1_CreatedUser1(object sender, EventArgs e)
        {
            CreateUserWizard wizard = (CreateUserWizard)sender;
            MembershipUser user = Membership.GetUser(wizard.UserName);
            if (user != null)
            {
                try
                {
                    Utilities.sendMail(Email.Text, "http://localhost:1163/ConfirmRegisteration.aspx?id=" + user.ProviderUserKey.ToString());
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.Message);
                }
            }
        }

        string st(int n)
        {
            if (n == 1)
                return "1st";
            if (n == 2)
                return "2nd";
            if (n == 3)
                return "3rd";
            else
                return(n+"th");

        }

        protected void btn_register_Click(object sender, EventArgs e)
        {
            string salt = db.CreateSalt(10);

            string hashedPW = db.CreatePasswordHash(Password.Text, salt);

            if (db.createNewUser(gucID.Text, FirstName.Text, MiddleName.Text, LastName.Text, Email.Text, hashedPW, salt, Int32.Parse(StudyGroup.SelectedValue), false))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "javascript:alert('Registration Successfull !');", true);
            }

            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "javascript:alert('Another user is already registered with this email/ID, please make sure of the data you entered or contact the administrator !');", true);
            
            }

        }

    }
}